home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 March: Reference Library / Dev.CD Mar 97 RL.toast / mac / Technical Documentation / develop / Additional Articles / Developing Symbiotic Apps / Symbiotic Samples / Symbiotic server source / flexibled & simpled / tridentd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-04  |  4.2 KB  |  166 lines  |  [TEXT/CWIE]

  1. /* main.c -- main routine for Status and Status-Who sample service daemons.
  2.  *
  3.  * Author: Chris Jalbert
  4.  * Copyright 1996 Apple Computer, Inc.
  5.  * All Rights Reserved.
  6.  *
  7.  * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF APPLE COMPUTER, INC.
  8.  * The copyright notice above does not evidence any actual or
  9.  * intended publication of such source code.
  10.  *
  11.  * History:
  12.  * 8/19/96    Chris Jalbert
  13.  *    Initial check in of new sample.
  14.  * 1/3/96    Chris Jalbert
  15.  *    Added code to test new AIX library function, PPCGetSelectMask.
  16.  */
  17.  
  18.  
  19. /* A lot of the following stuff works only when _ALL_SOURCE is defined
  20.  * because it is only available on BSD compatable systems.
  21.  */
  22.  
  23. #ifdef _AIX
  24. # ifndef _ALL_SOURCE
  25. #  define _ALL_SOURCE
  26. #  define USING_ALL_SOURCE
  27. # endif    /* _ALL_SOURCE */
  28.  
  29. # include <sys/select.h>    /* required for fd_set and select() */
  30. # include <stdio.h>        /* required for _P_tmpdir */
  31. # include <time.h>        /* required for struct timeval */
  32.  
  33. # ifdef USING_ALL_SOURCE
  34. #  undef _ALL_SOURCE
  35. #  undef USING_ALL_SOURCE
  36. # endif    /* USING_ALL_SOURCE */
  37. #else    /* _AIX */
  38. # define _P_tmpdir "Desktop"
  39. #endif    /* _AIX */
  40.  
  41.  
  42. /* It doesn't really matter at this point, but the rest can be compiled
  43.  * as strict POSIX or X/OPEN.
  44.  */
  45. #include <stdlib.h>
  46. #include <unistd.h>        /* required for chdir() */
  47.  
  48. /* MacOS header files. */
  49. #include <Types.h>
  50. #include <AppleEvents.h>
  51.  
  52. extern int WaitNextAppleEvent (EventRecord *, long *) ;
  53.  
  54. #include "tridentd.h"
  55. #include "debug.h"
  56.  
  57.  
  58. /******************************************************************************
  59.     ==>  Globals  <==
  60. ******************************************************************************/
  61. Boolean TimeToQuit = false ;
  62.  
  63. #if _DEBUG
  64. FILE *__DebugFile ;
  65. #endif
  66.  
  67.  
  68. /******************************************************************************
  69.     ==>  Global Functions  <==
  70. ******************************************************************************/
  71. #ifndef _AIX
  72. int WaitNextAppleEvent (
  73.     EventRecord    *event,
  74.     long        *lTimeOut)
  75.     {
  76.     WaitNextEvent (highLevelEventMask, event, (60 * *lTimeOut), NULL) ;
  77.     switch (event->what)
  78.         {
  79.         case nullEvent:
  80.             /* Might need to do some processing here. */
  81.             return 0 ;
  82.         case kHighLevelEvent:
  83.             return 1 ;
  84.         }
  85.     return 0 ;
  86.     }
  87. #endif    /* _AIX */
  88.  
  89.  
  90. /******************************************************************************
  91.     ==>  Main Function  <==
  92. ******************************************************************************/
  93. void main (
  94.     int            argc,
  95.     char        **argv)
  96.     {
  97.     OSErr         result ;
  98.  
  99.     chdir (_P_tmpdir) ;    /* so core files end up in /tmp */
  100.  
  101.     DBGSetup ((SIMPLIFIED ? "/tmp/simpled.debug" : "/tmp/flexibled.debug")) ;
  102.     DBGM ("Sleeping 15 seconds to allow dbx to attach...\n") ;
  103.     DBGPause (15) ;
  104.     DBGM ("Starting initialization procedures.\n") ;
  105.  
  106.     /* Install Apple event handlers. */
  107.     if (result = InitAEStuff (0L))
  108.         {
  109.         DBG ("Error %d during InitAEStuff().\n", result) ;
  110.         exit (result) ;
  111.         }
  112.  
  113.     /*** Stub or delete this call for simplified service daemons!! ***/
  114.     /* Register services with PPC Toolbox. */
  115.     if (result = InitPPCStuff ())
  116.         {
  117.         DBG ("Error %d during InitPPCStuff().\n", result) ;
  118.         exit (result) ;
  119.         }
  120.  
  121.     InitApp () ;
  122.  
  123.     while (!TimeToQuit && (result >= 0))
  124.         {
  125.         long        lTimeOut ;
  126.         EventRecord    event ;
  127.  
  128.         /*** Stub or delete this call for simplified service daemons!! ***/
  129.         /* Give the Mac OS version some time to deal with PPC overhead. */
  130.         result = PPCTimeSlice () ;
  131.  
  132.         /* Update the timers in the list of connections. */
  133.         lTimeOut = UpdateClients () ;
  134.  
  135.         DBG ("main(): setting timeout of %ld seconds.\n", lTimeOut) ;
  136.  
  137. #if defined(_AIX) && !SIMPLIFIED
  138.         {
  139.         extern Boolean GetMySelectMask (fd_set *) ;
  140.         fd_set            fdsMask ;
  141.         struct timeval    tsTimeout = { 0, 0 } ;
  142.  
  143.         PPCGetSelectMask (&fdsMask) ;
  144.         tsTimeout.tv_sec = lTimeOut ;
  145.         if (GetMySelectMask (&fdsMask))
  146.             {
  147.             DBGM ("main(): Making select call manually.\n") ;
  148.             select (FD_SETSIZE, &fdsMask, 0, 0, &tsTimeout) ;
  149.             }
  150.         }
  151. #endif    /* _AIX */
  152.  
  153.         /* Wait for an Apple Event or a new connection. */
  154.         if (0 < (result = WaitNextAppleEvent (&event, &lTimeOut)))
  155.             {
  156.             result = AEProcessAppleEvent (&event) ;
  157.             DBG ("main(): AEProcessAppleEvent() returned (%d)\n", result) ;
  158.             }
  159.         }
  160.  
  161.     /*** Stub or delete this call for simplified service daemons!! ***/
  162.     PPCShutDown () ;
  163.     DBGClose () ;
  164.     exit (0) ;
  165.     }
  166.